package 反射.T8;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class T8 {

    public static void main(String[] args) throws Exception {

        List<String> lines = FileUtils.readLines(new File("反射/T8/CatData1.txt"));
        List<Cat>list = new ArrayList<>();
        for (String line : lines) {
            String name = line.split(" ")[1].split("=")[1].replace("‘", "").replace("’", "");
            int age = Integer.parseInt(line.split(" ")[2].split("=")[1]);

            Class<?> aClass = Class.forName(line.split(" ")[0]);
            Object obj = aClass.newInstance();  //对象

            Method setName = aClass.getDeclaredMethod("setName", String.class);
            Method setAge = aClass.getDeclaredMethod("setAge", int.class);
            setName.setAccessible(true);
            setAge.setAccessible(true);
            setName.invoke(obj,name);
            setAge.invoke(obj,age);
            list.add((Cat) obj);
        }
        for (Cat c : list) {
            System.out.println(c);
        }
    }
}
